package com.wetao.lib_common.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;

/*
 * A bitmap memory cache implementation.
 *
 * @author Shintaro Katafuchi
 */
public final class BitmapLruCache {

    private static volatile LruCache<String, Bitmap> instance = null;
    public BitmapLruCache() {
        super();
    }

    public static LruCache getInstance(Context context) {
        if (instance == null) {
            synchronized (BitmapLruCache.class) {
                if (instance == null) {
                    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                    //获取Android系统分配给当前app的内存大小
                    int memoryClass = manager.getMemoryClass();

                    instance = new LruCache<String, Bitmap>(memoryClass*1024*1024/3){

                        @Override
                        protected int sizeOf(String key, Bitmap value) {

                            return value.getByteCount();
                        }
                    };
                }
            }
        }
        return instance;
    }

}
